package core;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.net.URLCodec;

/**
 * 
 * @author Meng Yuan wraper for paac's trip planer service.
 */

public class PAAC {

	private final String template = "http://www.portauthority.org/atisdnn/default.aspx?departure=departureV&arrival=arrivalV&month=monthV&day=dayV&year=yearV&hour=hourV&minute=minuteV&ampm=ampmV";
	public List<Itinerary> tripPlaner(String departure, String arrival,
			int month, int day, int year, int hour, int minute, String amPm)
			throws MalformedURLException, IOException, EncoderException {
		// construct the url string
		URLCodec codec=new URLCodec();
		departure=codec.encode(departure);
		arrival=codec.encode(arrival);
		String urlStr = this.template.replace("departureV", departure);
		urlStr = urlStr.replace("arrivalV", arrival);
		urlStr = urlStr.replace("monthV", "" + month);
		urlStr = urlStr.replace("dayV", "" + day);
		urlStr = urlStr.replace("yearV", "" + year);
		urlStr = urlStr.replace("hourV", "" + hour);
		urlStr = urlStr.replace("minuteV", "" + minute);
		urlStr = urlStr.replace("ampmV", amPm);
		Source source = new Source(new URL(urlStr));
		List<Itinerary> rs = new ArrayList<Itinerary>();
		List<Element> elements = source.getAllElements(HTMLElementName.OPTION);
		if(elements.size()!=0){
			rs=null;
			List<String> tips = new ArrayList<String>();
			for (Element element : elements){
				tips.add(element.getTextExtractor().toString().trim());
			}
			for(int i=0;i<tips.size();i++){
				System.out.println(tips.get(i));				
			}
		}else{
			elements = source.getAllElements(HTMLElementName.SPAN);
			Itinerary curIter = null;
			Route curRoute = null;
			for (Element element : elements) {
				String id = element.getAttributeValue("id");
				if (id != null && id.length() > 0) {
					if (id.contains("ResultsHeader")) {
						String content = element.getTextExtractor().toString()
								.trim();
						curIter = new Itinerary();
						rs.add(curIter);
						curIter.setItineraryName(content);
					} else if (id.contains("TripTime")) {
						String content = element.getTextExtractor().toString()
								.trim();
						curIter.setTripTime(content);
					} else if (id.contains("Transfer")) {
						String content = element.getTextExtractor().toString()
								.trim();
						if(content!=null&&content.length()>0){
						curIter.addTransfer(content);
						}
					} else if (id.endsWith("Route")) {
						String content = element.getTextExtractor().toString()
								.trim();
						curRoute = new Route();
						curIter.addRoute(curRoute);
						curRoute.setRouteName(content);
					} else if (id.contains("GetOn")) {
						String content = element.getTextExtractor().toString()
								.trim();
						curRoute.setGetOn(content);
					} else if (id.contains("OnTime")) {
						String content = element.getTextExtractor().toString()
								.trim();
						curRoute.setOnTime(content);
					} else if (id.contains("GetOff")) {
						String content = element.getTextExtractor().toString()
								.trim();
						curRoute.setGetOff(content);
					} else if (id.contains("OffTime")) {
						String content = element.getTextExtractor().toString()
								.trim();
						curRoute.setOffTime(content);
					}
				}
			}
		}	
		return rs;
	}
}
